<?php

namespace app\apiadmin\service\selled_order;

use app\apiadmin\model\selled_order\SelledOrderModel;
use app\apiadmin\service\data\CommStaticDictService;
use app\common\utils\Res;

class SelledOrderService
{

    /**
     * @author: zwx
     * @Description: 根据条件查询数据列表
     * @param {*} $param
     * @Date: 2024-05-12 14:50:39
     */    
    public static function getlist($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;
        $map = [];
        $map["o.del"] = 0;

        if (isset($param["refund_num"]) && !empty($param["refund_num"])) {
            $map["o.refund_num"] = $param["refund_num"];
        }
        if (isset($param["order_num"]) && !empty($param["order_num"])) {
            $map["o.order_num"] = $param["order_num"];
        }
        if (isset($param["coursename"]) && !empty($param["coursename"])) {
            $map[] = ['c.name','like','%'.$param["coursename"].'%'];
        }
        if (isset($param["from"]) && !empty($param["from"])) {
            $map["r.from"] = $param["from"];
        }
        if (isset($param["tel"]) && !empty($param["tel"])) {
            $map["r.tel"] = $param["tel"];
        }
        if (isset($param["state"]) && !empty($param["state"])) {
            $map["o.state"] = $param["state"];
        }
        if (isset($param["create_time1"]) && !empty($param["create_time1"]) ) {
            $map[] = ["o.create_time",'>=',$param["create_time1"]];
        }
        if (isset($param["create_time2"]) && !empty($param["create_time2"])) {
            $map[] = ["o.create_time",'<',$param["create_time2"]];
        }
        
        if (isset($param["pay_time1"]) && !empty($param["pay_time1"]) ) {
            $map[] = ["o.pay_time",'egt',$param["pay_time1"]];
        }
        if (isset($param["pay_time2"]) && !empty($param["pay_time2"])) {
            $map[] = ["o.pay_time",'lt',$param["pay_time2"]];
        }

        $findName = "o.*,c.name as coursename,r.fee,r.tel,r.from,r.pay_method,u.nickname,u.wx_avatarurl";

        $result = SelledOrderModel::pageMap($map,$findName, $offset, $limit);

        foreach ($result as $k => $v) {
            $result[$k]['pay_method'] = CommStaticDictService::FormatWithDict('pay_method',$result[$k]['pay_method']);
            $result[$k]['state'] = CommStaticDictService::FormatWithDict('selledstate',$result[$k]['state']); 
            $result[$k]['from'] = CommStaticDictService::FormatWithDict('from',$result[$k]['from']);
            $result[$k]['fee'] = number_format($result[$k]['fee']/100,2);
            $result[$k]['payer_refund'] = number_format($result[$k]['payer_refund']/100,2);
        }


        $total = SelledOrderModel::pageMapCount($map);
        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }
        

}